package com.jdz.gxq.info.admin.loan.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.jdz.gxq.info.admin.loan.bo.Loan;
import com.jdz.gxq.info.admin.loan.form.EditLoanForm;
import com.jdz.gxq.info.admin.loan.service.LoanService;
import com.jdz.gxq.info.common.action.CommonAction;

public class SaveLoanAction extends CommonAction {

	private LoanService loanService = null;
	 
	public ActionForward doExecute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		EditLoanForm editLoanForm = (EditLoanForm) form;
		
		Loan loan = editLoanForm.getLoan();
		
		// loan名重复验证(其他后台逻辑验证也类似处理)
		if (!loanService.checkLoanName(loan, 2)) {
			ActionMessages messages = new ActionMessages();
			messages.add("loan.enterpriseName", new ActionMessage("error.loanNameExisted"));
			addErrors(request, messages);
			return mapping.getInputForward();
		}
		
		loan.setModifiedBy(getUserId(request));
		
		loanService.updateLoan(loan);
		
		return mapping.findForward("success");
	}

	public void setLoanService(LoanService loanService) {
		this.loanService = loanService;
	}
}

